from ctypes import pointer
import pyautocad
from pyautocad.types import APoint
import turtle
import os

class Point:
    def __init__(self,x,y) -> None:
        self.x = x
        self.y = y

    def get(self) -> APoint:
        return APoint(self.x,self.y)

class Line:
    def __init__(self,p1,p2) -> None:
        self.p1 = p1
        self.p2 = p2

    def draw(self) -> None:
        acad.model.AddLine(self.p1.get(),self.p2.get())

class Rect:
    def __init__(self,minx,miny,maxx,maxy) -> None:
        self.minx = int(minx)
        self.miny = int(miny)
        self.maxx = int(maxx)
        self.maxy = int(maxy)

    def draw(self) -> None:
        Line(Point(self.minx,self.miny),Point(self.minx,self.maxy)).draw()
        Line(Point(self.maxx,self.miny),Point(self.maxx,self.maxy)).draw()
        Line(Point(self.maxx,self.miny),Point(self.minx,self.miny)).draw()
        Line(Point(self.maxx,self.maxy),Point(self.minx,self.maxy)).draw()
        
    def draw_lattice (self,is_row,size) -> None:
        if is_row:
            for x in range(self.minx+size,self.maxx,size):
                Line(Point(x,self.miny),Point(x,self.maxy)).draw()
        else:
            for y in range(self.miny+size,self.maxy,size):
                Line(Point(self.minx,y),Point(self.maxx,y)).draw()

class Circle:
    def __init__(self,p,r) -> None:
        self.p = p
        self.r = r

    def draw(self) -> None:
        acad.model.AddCircle(self.p.get(),self.r)

class Room:
    point = None
    length = None
    wide = None
    unit_points = []
    event_points = []
    

    def __init__(self,point,length,wide) -> None:

        pass

acad = pyautocad.Autocad()
Name = acad.doc.Name
#settings
TileSize = 128
MinSize = 32
#均值
Interval = TileSize *11
mainRooms = []
subRooms = []
allRects = []
RoadLines = []
RoadRects = []
Doundarts = []

_code = ''
# ImportData
def ImportWorldData (path):
    global _code
    try:
        with open(path,'r',encoding='utf-8') as fi :
            _str = fi.read()
    except IOError:
        print("没有找到文件目录\t" + path)
    else:
        _str = _str[:str.find(_str,'main_end')]
        _str_list = _str.split('\n')
        _code = ''
        for _str in _str_list :
            _str_list[_str_list.index(_str)] = _str[_str.find(']: ')+3:]
        for _str in _str_list :
            if _str == 'mainRooms = [' or _str == 'subRooms = [' or _str == 'RoadLines = [' or _str == 'RoadRects = [' or _str == 'Doundarts = [' or _str == 'allRects = [' or  _str == ']':
                _code += _str+'\n'
            if '[' in _str and ']' in _str and ',' in _str:
                _code += _str+',\n'
        pass
_paths = os.listdir(path="F:\\War3\\war3_WE\\破碎的记忆\\日志")
ImportWorldData("F:\\War3\\war3_WE\\破碎的记忆\\日志\\"+_paths[-1])
exec(_code)
print('路径数量 : %d\n主房间数量 : %d\n子房间数量 : %d'%(len(RoadLines),len(mainRooms),len(subRooms)))
#直接标记资源点与障碍物
def main():
    def clean():
        slt = acad.ActiveDocument.SelectionSets.Add("__clean")
        slt.Select(5)
        for obj in slt:
            obj.Delete()
        slt.Delete()
    clean()
    #BackgrndColor
    acad.ActiveDocument.Application.preferences.Display.GraphicsWinModelBackgrndColor = 0
    #Line_Layer
    LayerMainRooms = acad.ActiveDocument.Layers.Add("mainRooms")
    LayerMainRooms.color = 7
    LayerSubRooms = acad.ActiveDocument.Layers.Add("subRooms")
    LayerSubRooms.color = 141
    LayerRoadLines = acad.ActiveDocument.Layers.Add("roadLines")
    LayerRoadLines.color = 35
    LayerRoadRects = acad.ActiveDocument.Layers.Add("roadRects")
    LayerRoadRects.color = 141
    LayerDoundarts = acad.ActiveDocument.Layers.Add("Doundarts")
    LayerDoundarts.color = 2
    LayerAllRects = acad.ActiveDocument.Layers.Add("AllRects")
    LayerAllRects.color = 2
    
    acad.ActiveDocument.ActiveLayer = LayerMainRooms
    for room in mainRooms:
        _room = Rect(*room)
        _room.draw()
    Rect(-80*TileSize,-80*TileSize,80*TileSize,80*TileSize).draw()
    acad.ActiveDocument.ActiveLayer = LayerSubRooms
    for room in subRooms:
        _room = Rect(*room)
        _room.draw()
    acad.ActiveDocument.ActiveLayer = LayerRoadLines
    for RoadLine in RoadLines:
        _line = Line(Point(*RoadLine[0]),Point(*RoadLine[1]))
        _line.draw()
    acad.ActiveDocument.ActiveLayer = LayerRoadRects
    for RoadRect in RoadRects:
        _rect = Rect(*RoadRect)
        _rect.draw()
    acad.ActiveDocument.ActiveLayer = LayerDoundarts
    for Doundart in Doundarts:
        _line = Line(Point(*Doundart[0]),Point(*Doundart[1]))
        _line.draw()
    acad.ActiveDocument.ActiveLayer = LayerAllRects
    for room in allRects:
        _room = Rect(*room)
        _room.draw()
    
if __name__  == "__main__":
    if Name == "Drawing1.dwg":
        main()
    else:
        print('FileError')
